package com.example.bookmall.controller;

import com.example.bookmall.entity.Result;
import com.example.bookmall.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController

public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {

        //获取文件内容的输入流，写入到本地磁盘文件

        //originalFilename是原始文件的名字
        String originalFilename = file.getOriginalFilename();

        //保证文件的名字是唯一的，从而防止文件的覆盖
        String filename= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));

        String url = AliOssUtil.uploadFile(filename, file.getInputStream());

        return Result.success(url);
    }

}
